package co.com.umobile.application;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.app.Application;
import co.com.umobile.common.widget.ProgressHandler;
import co.com.umobile.domain.User;
import co.com.umobile.service.LoginServices;
import co.com.umobile.service.SessionServices;
import co.com.umobile.service.UpdateServices;
import co.com.umobile.service.impl.LoginServicesImpl;
import co.com.umobile.service.impl.SessionServicesImpl;
import co.com.umobile.service.impl.UpdateServicesImpl;

public class GlobalState extends Application {
	
	/** ip address. */
	private String ipAddress;
	
	/** ip port. */
	private String ipPort;

	/** Login Services. */
	private LoginServices loginServices;

	/** Session Services. */
	private SessionServices sessionServices;

	/** Update Services. */
	private UpdateServices updateServices;

	/** the user session. */
	private User userSession;

	/** the app version code. */
	private Integer versionCode;

	/** the version name of the app. */
	private String versionName;

	/** if the user cancel the authentication. */
	private volatile boolean loginDisable;

	/** a map with all the services handler. */
	private ConcurrentHashMap<Integer, ProgressHandler> handlerByService = new ConcurrentHashMap<Integer, ProgressHandler>();

	/** a map with all the parameter with value. */
	private ConcurrentHashMap<String, String> parameterHashMap = new ConcurrentHashMap<String, String>();

	/** the list of pending updates. */
	private ConcurrentLinkedQueue<Integer> updateQueue = new ConcurrentLinkedQueue<Integer>();

	/** the list of pending syncs. */
	private ConcurrentLinkedQueue<Integer> syncQueue = new ConcurrentLinkedQueue<Integer>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
	}

	/**
	 * @return the loginDisable
	 */
	public boolean isLoginDisable() {
		return loginDisable;
	}

	/**
	 * @param loginDisable
	 *            the loginDisable to set
	 */
	public void setLoginDisable(boolean loginDisable) {
		this.loginDisable = loginDisable;
	}

	/**
	 * @return the lastUserSession
	 */
	public User getLastUserSession() {
		if (userSession == null) {
			userSession = getSessionServices().getLastUserSession();
		}
		return userSession;
	}

	/**
	 * @param lastUserSession
	 *            the lastUserSession to set
	 */
	public void setLastUserSession(User lastUserSession) {
		this.userSession = lastUserSession;
	}

	/**
	 * @return the loginServices
	 */
	public LoginServices getLoginServices() {

		if (loginServices == null) {
			loginServices = new LoginServicesImpl();
		}

		return loginServices;
	}

	/**
	 * @return the sessionServices
	 */
	public SessionServices getSessionServices() {
		if (sessionServices == null) {
			sessionServices = new SessionServicesImpl();
		}
		return sessionServices;
	}

	/**
	 * @return the updateServices
	 */
	public UpdateServices getUpdateServices() {
		if (updateServices == null) {
			updateServices = new UpdateServicesImpl();
		}
		return updateServices;
	}

	/**
	 * @param updateServices
	 *            the updateServices to set
	 */
	public void setUpdateServices(UpdateServices updateServices) {
		this.updateServices = updateServices;
	}

	/**
	 * returns the server host.
	 * 
	 * @return host.
	 */
	public String getHost() {
		return ipAddress;
	}

	/**
	 * returns the server port.
	 * 
	 * @return port.
	 */
	public String getPort() {
		return ipPort;
	}
	

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public void setIpPort(String ipPort) {
		this.ipPort = ipPort;
	}

	/**
	 * @return the versionCode
	 */
	public Integer getVersionCode() {
		return versionCode;
	}

	/**
	 * @param versionCode
	 *            the versionCode to set
	 */
	public void setVersionCode(Integer versionCode) {
		this.versionCode = versionCode;
	}

	/**
	 * @return the versionName
	 */
	public String getVersionName() {
		return versionName;
	}

	/**
	 * @param versionName
	 *            the versionName to set
	 */
	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	/**
	 * @return the handlerByService
	 */
	public ConcurrentHashMap<Integer, ProgressHandler> getHandlerByService() {
		return handlerByService;
	}

	/**
	 * @param handlerByService
	 *            the handlerByService to set
	 */
	public void setHandlerByService(ConcurrentHashMap<Integer, ProgressHandler> handlerByService) {
		this.handlerByService = handlerByService;
	}

	/**
	 * @return the parameterHashMap
	 */
	public ConcurrentHashMap<String, String> getParameterHashMap() {
		return parameterHashMap;
	}

	/**
	 * @param parameterHashMap
	 *            the parameterHashMap to set
	 */
	public void setParameterHashMap(ConcurrentHashMap<String, String> parameterHashMap) {
		this.parameterHashMap = parameterHashMap;
	}

	/**
	 * @return the updateQueue
	 */
	public ConcurrentLinkedQueue<Integer> getUpdateQueue() {
		return updateQueue;
	}

	/**
	 * @param updateQueue
	 *            the updateQueue to set
	 */
	public void setUpdateQueue(ConcurrentLinkedQueue<Integer> updateQueue) {
		this.updateQueue = updateQueue;
	}

	/**
	 * @return the syncQueue
	 */
	public ConcurrentLinkedQueue<Integer> getSyncQueue() {
		return syncQueue;
	}

	/**
	 * @param syncQueue
	 *            the syncQueue to set
	 */
	public void setSyncQueue(ConcurrentLinkedQueue<Integer> syncQueue) {
		this.syncQueue = syncQueue;
	}

	/**
	 * @return the userSession
	 */
	public User getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession
	 *            the userSession to set
	 */
	public void setUserSession(User userSession) {
		this.userSession = userSession;
	}

}
